package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private LogService logService;
    @Override
    public Map<String, Object> listSupplier(Integer page, Integer rows, String supplierName) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page-1) * rows;
        List<Supplier> supplierList = supplierDao.getSupplierList(offSet, rows, supplierName);
        map.put("rows",supplierList);
        map.put("total",supplierList.size());
        logService.save(new Log(Log.SELECT_ACTION,"分页查询供应商"));
        return map;
    }

    @Override
    public void saveSupplier(Supplier supplier ,Integer supplierId) {
        //先判断什么时候是修改，什么时候是保存
        if(supplierId!=null){
            supplierDao.updateSupplier(supplier);
        }else{
            supplierDao.saveSupplier(supplier);
        }
    }

    @Override
    public void deleteSupplier(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            supplierDao.deleteSupplier(id);
        }
    }
}
